package demo.Thread;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/10-08-10-16:35
 * @Description：demo.Thread
 */
public class Account {
    //账号余额
    private int balance;
    public Account(int balnace) {
        this.balance = balnace;
    }
    //存钱
    public void pos(int money){
        balance = balance + money;
        synchronized (this){
            this.notifyAll();
        }
    }
    //消费
    public void wit(int money){
        synchronized (this){
            if(money > balance){
                System.out.println("余额不足，停止消费");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                balance = balance -money;
                System.out.println("当前余额： "  + balance);
            }
        }
    }

}
